<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
	xmlns:hbm="urn:nhibernate-mapping-2.2"
	xmlns:ms="urn:schemas-microsoft-com:xslt"
	xmlns:helper="urn:helper">
	<xsl:output method="text" encoding="UTF-8"/>

	<xsl:param name="use-dto">
		<xsl:choose>
			<xsl:when test="string(/hbm:hibernate-mapping/hbm:meta[@attribute='dto'])='false'">false</xsl:when>
			<xsl:otherwise>false</xsl:otherwise>
		</xsl:choose>
	</xsl:param>
	<xsl:param name="def-access">
		<xsl:choose>
			<xsl:when test="count(./hbm:hibernate-mapping/@default-access)>0">
				<xsl:value-of select="./hbm:hibernate-mapping/@default-access"/>
			</xsl:when>
			<xsl:otherwise>property</xsl:otherwise>
		</xsl:choose>
	</xsl:param>
	<xsl:variable name="namespace" select="/hbm:hibernate-mapping/@namespace"/>
	<xsl:variable name="tab" select="string('&#32;&#32;&#32;&#32;')"/>
	<xsl:variable name="tab2" select="concat($tab, $tab)"/>
	<xsl:variable name="tab3" select="concat($tab, $tab, $tab)"/>
	<xsl:variable name="newline" select="string('&#10;')"/>

	<xsl:variable name="type-info">
		<type name="Timestamp" net="byte[]"/>
		<type name="Ticks" net="DateTime" value="true"/>
		<type name="TrueFalse" net="bool" value="true"/>
		<type name="YesNo" net="bool" value="true"/>
		<type name="Binary" net="byte[]"/>
		<type name="Byte" net="byte" value="true"/>
		<type name="BinaryBlob" net="byte[]"/>
		<type name="String" net="string"/>
		<type name="StringClob" net="string"/>
		<type name="AnsiString" net="string"/>
		<type name="Serializable" net="object"/>
		<type name="Int32" net="int" value="true"/>
		<type name="Decimal" net="decimal" value="true"/>
		<type name="Char" net="char" value="true"/>
	</xsl:variable>

	<xsl:variable name="collection-info">
		<bag declaration="IList" instance="List" />
		<list declaration="IList" instance="List" />
		<map declaration="IDictionary" instance="Dictionary" />
		<set prefix="ISet" instance="HashedSet"/>
	</xsl:variable>

	<xsl:template name="comment">
		<xsl:param name="text">Комментарий не задан</xsl:param>
		<xsl:param name="level">2</xsl:param>
		<xsl:variable name="level-str">
			<xsl:choose>
				<xsl:when test="$level=0"></xsl:when>
				<xsl:when test="$level=2">
					<xsl:value-of select="concat($tab, $tab)"/>
				</xsl:when>
				<xsl:when test="$level=3">
					<xsl:value-of select="concat($tab, $tab, $tab)"/>
				</xsl:when>
				<xsl:when test="$level=4">
					<xsl:value-of select="concat($tab, $tab, $tab, $tab)"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="$tab"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<xsl:value-of select="concat($newline, $level-str, '/// &lt;summary&gt;', $newline, $level-str, '/// ', $text, $newline, $level-str, '/// &lt;/summary&gt;')"/>
	</xsl:template>

	<xsl:template name="class2code">
		<xsl:param name="name"/>
		<xsl:param name="not-null"/>
		<xsl:choose>
			<xsl:when test="contains($name, ',') and starts-with($name, $namespace)">
				<xsl:value-of select="substring-after(substring-before($name, ','), concat($namespace, '.'))"/>
			</xsl:when>
			<xsl:when test="contains($name, ',')">
				<xsl:value-of select="substring-before($name, ',')"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="$name"/>
			</xsl:otherwise>
		</xsl:choose>
		<xsl:if test="$not-null='false'">?</xsl:if>
	</xsl:template>

	<xsl:template name="type2type">
		<xsl:param name="type"/>
		<xsl:param name="not-null">true</xsl:param>
		<xsl:variable name="result" select="ms:node-set($type-info)/type[@name=$type]/@net"/>
		<xsl:choose>
			<xsl:when test="string($result)!=''">
				<xsl:value-of select="$result"/>
				<xsl:if test="$not-null='false' and ms:node-set($type-info)/type[@name=$type]/@value='true'">?</xsl:if>
			</xsl:when>
			<xsl:otherwise>
				<xsl:call-template name="class2code">
					<xsl:with-param name="name" select="$type"/>
					<xsl:with-param name="not-null" select="$not-null"/>
				</xsl:call-template>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	
	<!-- получить списковый тип -->
	<xsl:template name="InstanceListType">
		<xsl:param name="type"/>
		<xsl:param name="index-type"/>
		<xsl:param name="collection">bag</xsl:param>
		<xsl:param name="dto">false</xsl:param>
		<xsl:variable name="info" select="ms:node-set($collection-info)/*[name(.)=$collection]"/>
		<xsl:choose>
			<xsl:when test="$collection='map'">
				<xsl:value-of select="concat('Dictionary&lt;', $index-type, ', ', $type, '&gt;')"/>
			</xsl:when>
			<xsl:when test="$collection='array' or $dto='true'">
				<xsl:value-of select="concat($type, '[]')"/>
			</xsl:when>
			<xsl:when test="$info">
				<xsl:value-of select="concat($info/@instance, '&lt;', $type, '&gt;')"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="concat('List&lt;', $type, '&gt;')"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

</xsl:stylesheet>
